package com.etong.citymap.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.etong.citymap.bean.AssignmentBean;
import com.etong.citymap.bean.ImageBean;
import com.etong.citymap.bean.ProblemBean;
import com.etong.citymap.connect.HttpSendData;
import com.etong.citymap.db.impl.AppendixDaoImp;
import com.etong.citymap.db.impl.SentenceDaoImp;
import com.etong.citymap.db.util.CityApplication;
import com.etong.citymap.module.AppendixBean;
import com.etong.citymap.module.SentenceBean;
import com.etong.citymap.util.ActivityUtil;
import com.etong.citymap.util.Status;
import com.etong.citymap.util.StringUtil;
import com.etong.citymap.util.log.LogUtil;
import com.supermap.data.Point2D;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 问题上报界面
 */
public class ProblemUploadActivity extends Activity implements OnClickListener, OnItemSelectedListener {
	public int MAP_RETURN_CODE = 100;
	Button pro_but_back;
	AlertDialog load;
	private static ProblemBean[] cacheresult;
	private static String[] problem_typestrings;
	private static String[] problem_daleistrings;
	private static String[] problem_xiaoleistrings;
	Spinner problem_type;
	Spinner problem_dalei;
	Spinner problem_xiaolei;
	Button pro_locationbut;
	EditText problem_zuobiao;
	EditText problem_situation;
	EditText problem_difang;
	LinearLayout pro_appendix;
	LinearLayout pro_display;
	LinearLayout pro_menu;
	TextView pro_display_count;
	TextView pro_title;
	RelativeLayout pro_display_count_bg;
	LinearLayout pro_but_button_back;
	LinearLayout problem_shushi_bg;
	ToggleButton problem_shushi;
	PopupWindow popupWindow;
	RelativeLayout problem_bgid;
	LinearLayout problemmenu_save;
	LinearLayout problemmenu_upload;
	//LinearLayout problemmenu_delete;
	long saveid;
	String Fjname = "";
	boolean issave;
	SentenceBean sentenceBean; // 本地保存 传送过来
	AssignmentBean bean;
	int historytype;
	boolean IsFuCha;
	private SentenceDaoImp sentenceDaoImp = new SentenceDaoImp(
			ProblemUploadActivity.this);
	private AppendixDaoImp appendixDaoImp = new AppendixDaoImp(
			ProblemUploadActivity.this);
	private ArrayAdapter<String> problem_typeadapter;
	private ArrayAdapter<String> problem_daleiadapter;
	private ArrayAdapter<String> problem_xiaoleiadapter;
	AlertDialog uploadalert;
	AlertDialog locationalert;
	private int typepostion;

	private Point2D locationPoint;
	private CityApplication cityApplication;

	private String commName;
	private String bgcode;
	
	Handler handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			if (msg.what == 2) {
				load.dismiss();
				pro_appendix.setVisibility(LinearLayout.GONE);
				problem_typeadapter = new ArrayAdapter<String>(
						ProblemUploadActivity.this, R.layout.myspinner,
						problem_typestrings);
				problem_typeadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				problem_type.setAdapter(problem_typeadapter);
				problem_type.setSelection(0);//默认选择事件
				problem_daleiadapter = new ArrayAdapter<String>(
						ProblemUploadActivity.this, R.layout.myspinner,
						problem_daleistrings);
				problem_daleiadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				problem_dalei.setAdapter(problem_daleiadapter);
				problem_xiaoleiadapter = new ArrayAdapter<String>(
						ProblemUploadActivity.this, R.layout.myspinner,
						problem_xiaoleistrings);
				problem_xiaoleiadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				problem_xiaolei.setAdapter(problem_xiaoleiadapter);
				pro_title.setText("问题上报");
				if (bean != null) {
					problem_type.setEnabled(false);
					problem_dalei.setEnabled(false);
					problem_xiaolei.setEnabled(false);
					if (sentenceBean == null) {
						problem_shushi_bg.setVisibility(LinearLayout.VISIBLE);
						//problemmenu_delete.setVisibility(LinearLayout.GONE);
						problemmenu_upload.setVisibility(LinearLayout.GONE);
						IsFuCha = true;
						pro_title.setText("任务核查");
					} else {
						problemmenu_save.setVisibility(LinearLayout.GONE);
						pro_appendix.setVisibility(LinearLayout.GONE);
					}
					problem_situation.setText(bean.getSituation());

					locationPoint = new Point2D();
					locationPoint.setX(Double.valueOf(bean.getX()));
					locationPoint.setY(Double.valueOf(bean.getY()));
					String text = "经度:" + locationPoint.getX() + " 纬度"
							+ locationPoint.getY();
					problem_zuobiao.setText(text);
					for (int i = 0; i < cacheresult.length; i++) {
						if (cacheresult[i].getId() == Integer.valueOf(bean.getType())) {
							gettype(i);
							problem_type.setSelection(i);
							ProblemBean[] dalei = cacheresult[i].getChild();
							for (int t = 0; t < dalei.length; t++) {
								if (dalei[t].getId() == Integer.valueOf(bean.getBigclass())) {
									getdalei(t);
									problem_dalei.setSelection(t);
									ProblemBean[] xiaolei = dalei[t].getChild();
									for (int y = 0; y < xiaolei.length; y++) {
										if (xiaolei[y].getId() == Integer.valueOf(bean.getSmallclass())) {
											problem_xiaolei.setSelection(y);
											break;
										}
									}
									break;
								}
							}
							break;
						}
					}

				}
			} else if (msg.what == 3) { // gps
				if (locationPoint != null) {
					String text = "经度:" + locationPoint.getX() + " 纬度"
							+ locationPoint.getY();
					problem_zuobiao.setText(text);
				}

			} else if (msg.what == 5) {// 数据上传和保存
				if (msg.arg1 == 6) {
					uploadalert.dismiss();
					Toast.makeText(ProblemUploadActivity.this, "上传完毕", Toast.LENGTH_SHORT).show();
					finish();
				} else {
					String data = msg.getData().getString("msg");
					uploadalert.setMessage(data);
				}

			} else if (msg.what == 6) {
				uploadalert.dismiss();
				Toast.makeText(ProblemUploadActivity.this, msg.obj.toString(),
						Toast.LENGTH_SHORT).show();
			}else if(msg.what==8){
				/*Bundle bundle = new Bundle();
				if(locationPoint!=null){
					bundle.putDouble("x", locationPoint.getX());
					bundle.putDouble("y", locationPoint.getY());
				}*/
				cityApplication.setPrePoint(locationPoint);
				if(bean==null || (sentenceBean!=null && sentenceBean.getFlag()==Status.ProjectType_SB && sentenceBean.getUploadState()==Status.ERORR_0101))
					cityApplication.setReturnResult(true);//bundle.putBoolean("result", true);
				ActivityUtil.ToActivity(ProblemUploadActivity.this, MapActivity.class);
				locationalert.dismiss();
			}
			return false;
		}
	});

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.problemupload);
		cityApplication = (CityApplication)getApplication();
		load = new ProgressDialog(this);
		load.setTitle(null);
		load.setMessage("请稍等,正在加载数据...");
		load.setCanceledOnTouchOutside(false);
		load.show();
		uploadalert = new ProgressDialog(this);
		uploadalert.setCanceledOnTouchOutside(false);
		uploadalert.setTitle(null);
		locationalert=new ProgressDialog(this);
		locationalert.setMessage("正在打开中,请稍等");
		locationalert.setCanceledOnTouchOutside(false);
		locationalert.setTitle(null);
		issave = false;
		init();
		historyly();
		new Loading();
		View view = LayoutInflater.from(this).inflate(R.layout.problemmenu,
				null);
		popupWindow = new PopupWindow(view);
		popupWindow.setAnimationStyle(R.style.popwin_anim_style);
		popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
		popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
		problemmenu_save = (LinearLayout) view.findViewById(R.id.problemmenu_save);
		problemmenu_upload = (LinearLayout) view.findViewById(R.id.problemmenu_upload);
		if(bean!=null){
			problemmenu_upload.setVisibility(View.VISIBLE);
		}
		
		//problemmenu_delete = (LinearLayout) view.findViewById(R.id.problemmenu_delete);
		problemmenu_save.setOnClickListener(this);
		problemmenu_upload.setOnClickListener(this);
		//problemmenu_delete.setOnClickListener(this);
		IsFuCha = false;
	}

	void historyly() {
		
		Intent intent = this.getIntent();
		int historytype = intent.getIntExtra("historytype", -1);
		bean = (AssignmentBean) intent.getSerializableExtra("bean");
		if (historytype == Status.SaveState_t1) {
			pro_menu.setVisibility(LinearLayout.GONE);
			pro_appendix.setVisibility(LinearLayout.GONE);
			
			sentenceBean = cityApplication.getCacheSentence();
			pro_display.setVisibility(LinearLayout.VISIBLE);
			int count = sentenceBean.getAppendixBeans().size();
			pro_display_count_bg.setVisibility(LinearLayout.VISIBLE);
			if (count > 0) {
				pro_display_count_bg.setVisibility(LinearLayout.VISIBLE);
				pro_display_count.setText(count + "");
			} else {
				pro_display_count_bg.setVisibility(LinearLayout.VISIBLE);
				pro_display_count.setText("");
			}

		} else if (historytype == Status.SaveState_t2) {
			issave = true;
			sentenceBean = cityApplication.getCacheSentence();
			pro_display.setVisibility(LinearLayout.VISIBLE);
			pro_display_count_bg.setVisibility(LinearLayout.VISIBLE);
		
			int count = sentenceBean.getAppendixBeans().size();
			pro_display_count_bg.setVisibility(LinearLayout.VISIBLE);
			if (count > 0) {
				pro_display_count_bg.setVisibility(LinearLayout.VISIBLE);
				pro_display_count.setText(count + "");
			} else {
				pro_display_count_bg.setVisibility(LinearLayout.VISIBLE);
				pro_display_count.setText("");
			}
		}

	}

	void init() {  //初始化数据
		pro_but_back = (Button) findViewById(R.id.pro_but_back);
		pro_but_back.setOnClickListener(this);
		problem_type = (Spinner) findViewById(R.id.problem_type);
		problem_dalei = (Spinner) findViewById(R.id.problem_dalei);
		problem_xiaolei = (Spinner) findViewById(R.id.problem_xiaolei);
		problem_xiaolei.setOnItemSelectedListener(this);
		problem_dalei.setOnItemSelectedListener(this);
		problem_type.setOnItemSelectedListener(this);
		pro_locationbut = (Button) findViewById(R.id.pro_location);
		pro_locationbut.setOnClickListener(this);
		problem_zuobiao = (EditText) findViewById(R.id.problem_zuobiao);
		pro_appendix = (LinearLayout) findViewById(R.id.pro_appendix);
		pro_appendix.setOnClickListener(this);
		pro_display = (LinearLayout) findViewById(R.id.pro_display);
		pro_display.setOnClickListener(this);
		pro_display_count = (TextView) findViewById(R.id.pro_display_count);
		pro_display_count_bg = (RelativeLayout) findViewById(R.id.pro_display_count_bg);
		pro_menu = (LinearLayout) findViewById(R.id.pro_menu);
		pro_menu.setOnClickListener(this);
		pro_but_button_back = (LinearLayout) findViewById(R.id.pro_but_button_back);
		pro_but_button_back.setOnClickListener(this);
		problem_situation = (EditText) findViewById(R.id.problem_situation);
		problem_difang = (EditText) findViewById(R.id.problem_difang);
		problem_shushi_bg = (LinearLayout) findViewById(R.id.problem_shushi_bg);
		problem_shushi = (ToggleButton) findViewById(R.id.problem_shushi);
		problem_bgid = (RelativeLayout) findViewById(R.id.problem_bgid);
		problem_bgid.setOnClickListener(this);
		pro_title=(TextView) findViewById(R.id.pro_title);

	}

	@Override
	protected void onResume() {  //判断当前要显示的图片个数
		if(cityApplication.isReturnResult()){
			locationPoint = cityApplication.getLocPoint();
			bgcode = cityApplication.getBgCode();
			commName = cityApplication.getCommName();
			handler.sendEmptyMessage(3);
		}
		cityApplication.setReturnResult(false);
		ImageBean[] ilns = cityApplication.getImageList_Net();
		ImageBean[] ils = cityApplication.getImageList();
		if (ils != null || ilns != null) {
			int count = 0;
			pro_display.setVisibility(LinearLayout.VISIBLE);
			if (ils != null) {
				for (int i = 0; i < ils.length; i++) {
					if (ils[i].isIsSelect()) {
						++count;
					}
				}
			}
			if (ilns != null) {

				if (ilns != null) {
					for (int i = 0; i < ilns.length; i++) {
						if (ilns[i].isIsSelect()) {
							++count;
						}
					}
				}

			}
			if (count > 0) {
				pro_display_count_bg.setVisibility(LinearLayout.VISIBLE);
				pro_display_count.setText(count + "");
			} else {
				pro_display_count_bg.setVisibility(LinearLayout.VISIBLE);
				pro_display_count.setText("");
			}
		}

		super.onResume();
	}

	/*protected void onActivityResult(int requestCode, int resultCode, Intent data) {  //定位返回
		if(requestCode == MAP_RETURN_CODE){
			if(resultCode==RESULT_CANCELED && data!=null){
				Bundle bundle = data.getExtras(); 
				Double x = bundle.getDouble("x");
				Double y = bundle.getDouble("y");
				locationPoint = new Point2D(x, y);
				bgcode = bundle.getString("bgcode");
				commName = bundle.getString("commName");
				handler.sendEmptyMessage(3);
			}
		}
	} */

	@Override
	public void onClick(View v) {
		//ImageBean[] ilns = cityApplication.getImageList_Net();
		ImageBean[] ils = cityApplication.getImageList();
		if (v == pro_but_back || pro_but_button_back == v) {  //关闭
			finish();  
		}
		if (pro_locationbut == v) {                         //定位
			locationalert.show();
			new Loctioning();
		}
		if (pro_appendix == v) {               //拍照 
			ActivityUtil.ToActivity(ProblemUploadActivity.this,	CamerDisplayActivity.class);
		}
		if (pro_display == v) {   //图片预览
			Intent intent = new Intent(this, CamerDisplayActivity.class);
			if (sentenceBean != null) {
				if (sentenceBean.getId() > 0) {
					ArrayList<String> camernames = new ArrayList<String>();
					for (AppendixBean appendixBean : sentenceBean.getAppendixBeans()) {
						camernames.add(appendixBean.getPath());
					}
					intent.putStringArrayListExtra("list", camernames);
					intent.putExtra("iszhidu", true);
				}
			}
			startActivity(intent);
		}
		if (pro_menu == v) {         //菜单
			int[] location = new int[2];
			v.getLocationOnScreen(location);
			int jianq = 120;
			if (IsFuCha) {
				jianq = 160;
			}
			if (sentenceBean != null) {
				if (sentenceBean.getId() > 0) {
					jianq =120;
				}
			}
			popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0],
					location[1] - jianq);
		}
		if (problem_bgid == v) {//隐藏菜单
			popupWindow.dismiss();
		}

		if (problemmenu_save == v) {  //数据保存
			if (ils == null||ils.length<=0) {
				Toast.makeText(ProblemUploadActivity.this, "请先拍照",Toast.LENGTH_SHORT).show();
				return;
			} 
			if(locationPoint==null){
				Toast.makeText(ProblemUploadActivity.this, "请先定位！",Toast.LENGTH_SHORT).show();
				return;
			}

			if(bean==null)
				if((StringUtil.IsNull(bgcode) || StringUtil.IsNull(commName))  ){
					Toast.makeText(ProblemUploadActivity.this, "定位有误，请重新定位！",Toast.LENGTH_SHORT).show();
					return;
				}
			if (!issave) {
				sentenceBean = new SentenceBean();
				if (bean != null) {
					if (bean.getPhoto1() != null) {
						sentenceBean.setPrjid(bean.getPrjid());
						sentenceBean.setPrjcheckid(bean.getPrjcheckid());
						sentenceBean.setFlag(bean.getFlag());
						if (problem_shushi.isChecked()) {
							sentenceBean.setShushi(Status.shushi_off);
						} else {
							sentenceBean.setShushi(Status.shushi_on);
						}
					}
				} else {
					sentenceBean.setFlag(Status.ProjectType_SB);
				}
				sentenceBean.setPosition("芜湖县");
				ProblemBean type = cacheresult[problem_type
				                               .getSelectedItemPosition()];
				sentenceBean.setCommunityid(commName);
				sentenceBean.setBgcode(bgcode);
				sentenceBean.setEventtype(type.getId() + "");
				ProblemBean[] dalei = type.getChild();
				ProblemBean selectdalei = dalei[problem_dalei
				                                .getSelectedItemPosition()];
				sentenceBean.setBigclass(selectdalei.getId() + "");
				ProblemBean[] xiaolei = selectdalei.getChild();
				sentenceBean.setSmallclass(xiaolei[problem_xiaolei
				                                   .getSelectedItemPosition()].getId() + "");
				sentenceBean.setSituation(problem_situation.getText()
						.toString());
				sentenceBean.setSuperid(cityApplication.getUserId());
				sentenceBean.setCode(cityApplication.getUserCode());
				sentenceBean.setXcoor(locationPoint.getX()+ "");
				sentenceBean.setYcoor(locationPoint.getY() + "");
				sentenceBean.setSaveState(Status.SaveState_t1);
				//sentenceBean.setShushi(Status.shushi_weisheding);
				sentenceBean.setCreatedate(new Date());
				sentenceBean.setUploadState(0);
				sentenceBean.setUploadSum(0);
				saveid = sentenceDaoImp.insert(sentenceBean);
				if (saveid > 0) {
					
					Toast.makeText(ProblemUploadActivity.this, "保存成功",
							Toast.LENGTH_SHORT).show();
					if (ils != null) {
						for (int i = 0; i < ils.length; i++) {
							if (ils[i].isIsSelect()) {
								AppendixBean appendixBean = new AppendixBean();
								appendixBean.setCaseid(saveid);
								appendixBean.setPath(ils[i].getPathname());
								appendixBean
								.setState(Status.AdjunctUpload_Save);
								long cacheid = appendixDaoImp
										.insert(appendixBean);
								if (cacheid > 0) {
									Fjname += appendixBean.getPath() + ",";
								}

							}
						}
					}
					issave = true;
				    //Intent intent=new Intent(ProblemUploadActivity.this,UploadSerivice.class);
					//startService(intent);
				    this.finish();
				}
			} else {
				Toast.makeText(ProblemUploadActivity.this, "已经保存成功",
						Toast.LENGTH_SHORT).show();
			}

		}
		if (problemmenu_upload == v) {   //数据上传
			if(sentenceBean==null || sentenceBean.getFlag()!=Status.ProjectType_HC)
				if((StringUtil.IsNull(sentenceBean.getBgcode()) || StringUtil.IsNull(sentenceBean.getCommunityid()))  ){
					Toast.makeText(ProblemUploadActivity.this, "定位有误，请重新定位！",Toast.LENGTH_SHORT).show();
					return;
				}
			if (!issave) {
				Toast.makeText(ProblemUploadActivity.this, "请先保存",Toast.LENGTH_SHORT).show();
			} else if(sentenceBean != null && sentenceBean.getUploadState()>=0){
				Toast.makeText(ProblemUploadActivity.this, "此案卷正在后台上报，请勿重复提交！",Toast.LENGTH_SHORT).show();
			}else { 
				if (ils == null) {
					if (sentenceBean != null) {
						if (sentenceBean.getId() > 0) {
							List<AppendixBean> appendixBeans = sentenceBean.getAppendixBeans();
							if (appendixBeans != null) {
								ils = new ImageBean[appendixBeans.size()];
								for (int i = 0; i < ils.length; i++) {
									ils[i] = new ImageBean();
									ils[i].setIsSelect(true);
									ils[i].setPathname(appendixBeans.get(i).getPath());
								}
								cityApplication.setImageList(ils);
							}
						}
					}
				}
				if (ils == null||ils.length<=0) {
					Toast.makeText(ProblemUploadActivity.this, "请先拍照",Toast.LENGTH_SHORT).show();
				} else {
					sentenceBean.setXcoor(locationPoint.getX()+ "");
					sentenceBean.setYcoor(locationPoint.getY() + "");
					if((sentenceBean!=null && sentenceBean.getFlag()==Status.ProjectType_SB && sentenceBean.getUploadState()==Status.ERORR_0101)){
						if(StringUtil.IsNotNull(bgcode) && StringUtil.IsNotNull(commName)){
						sentenceBean.setBgcode(bgcode);
						sentenceBean.setCommunityid(commName);
						}
					}
					popupWindow.dismiss();
					uploadalert.show();
					new UploadData();
				}
			}
		}
		/*if (problemmenu_delete == v) {   //数据删除
			AlertDialog.Builder builder = new Builder(ProblemUploadActivity.this);
			builder.setTitle("提示");
			builder.setMessage("你确定要删除数据?");
			builder.setPositiveButton("取消", null);
			builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						String url = "supervisor/ashx/delprjcheck.ashx";
						Map<String, String> map = new HashMap<String, String>();
						map.put("prjcheckstring", bean.getPrjcheckid());
						String result = HttpSendData.SendGet(url, map, "utf-8",cityApplication.getHTTPURL());
						if (result == null) {
							Toast.makeText(ProblemUploadActivity.this, "连接服务器出错！",
									Toast.LENGTH_SHORT).show();
						} else {
							switch (Integer.valueOf(result)) {
							case -1: {
								Toast.makeText(ProblemUploadActivity.this,
										"服务器处理出错，请稍候处理。", Toast.LENGTH_SHORT).show();
								break;
							}
							case -3: {
								Toast.makeText(ProblemUploadActivity.this, "连接参数出错！",
										Toast.LENGTH_SHORT).show();
								break;
							}
							default: {
								Toast.makeText(ProblemUploadActivity.this, "删除成功",
										Toast.LENGTH_SHORT).show();
								finish();
								break;
							}
							}
						}
					} catch (Exception e) {
						LogUtil.e(e);
					}
				}});
			builder.show();
		}*/
	}

	class UploadData extends Thread {   //上传数据
		public UploadData() {
			start();
		}

		@Override
		public void run() {
			ImageBean[] ibs = cityApplication.getImageList();
			if (issave) {
				if (Fjname.equals("")) {
					if (ibs != null) {
						for (int i = 0; i < ibs.length; i++) {
							if (ibs[i].isIsSelect()) {
								Fjname += ibs[i].getPathname() + ",";
							}
						}
					}

				}
				Message message = new Message();
				message.what = 5;
				message.getData().putString("msg", "正在上传数据");
				handler.sendMessage(message);
				String url = "supervisor/ashx/WorkStart.ashx";
				Map<String, String> map = new HashMap<String, String>();
				map.put("Prjid", sentenceBean.getPrjid());
				map.put("Prjcheckid", sentenceBean.getPrjcheckid());
				map.put("Position", "芜湖县");
				map.put("Eventtype", sentenceBean.getEventtype());
				map.put("Bigclass", sentenceBean.getBigclass());
				map.put("Smallclass", sentenceBean.getSmallclass());
				map.put("Situation", sentenceBean.getSituation());
				String cache = Fjname.substring(0, Fjname.length() - 1);
				map.put("Fjname", cache);
				map.put("Flag", sentenceBean.getFlag() + "");
				map.put("Superid", cityApplication.getUserId() + "");
				map.put("Code", cityApplication.getUserCode() + "");
				map.put("Bgcode", sentenceBean.getBgcode());
				map.put("Objcode", sentenceBean.getObjcode());
				map.put("Communityid", sentenceBean.getCommunityid());
				map.put("Units", sentenceBean.getUnits());
				map.put("Xcoor", sentenceBean.getXcoor());
				map.put("Ycoor", sentenceBean.getYcoor());
				try {
					String result = HttpSendData.SendGet(url, map, "utf-8",cityApplication.getHTTPURL());
					if (StringUtil.IsNull(result)) {
						message = new Message();
						message.obj = "连接服务器出错";
						message.what = 6;
						handler.sendMessage(message);
					} else {
						switch (Integer.valueOf(result)) {
						case -3: {
							message = new Message();
							message.obj = "服务器出错";
							message.what = 6;
							handler.sendMessage(message);
							break;
						}
						case -1: {
							message = new Message();
							message.what = 6;
							message.obj = "服务器出错";
							handler.sendMessage(message);
							break;
						}
						case -4: {
							message = new Message();
							message.obj = "已在其他机器登录";
							message.what = 6;
							handler.sendMessage(message);
							break;
						}
						case -2: {
							message = new Message();
							message.what = 6;
							message.obj = "上报坐标不在所管辖的范围内";
							handler.sendMessage(message);
							break;
						}
						default: {
							message = new Message();
							message.what = 5;
							message.getData().putString("msg", "数据上传成功,正在上传附件");
							Log.i("System.out.p","数据上传成功,正在上传附件");
							handler.sendMessage(message);
							if (StringUtil.IsNull(result)) {
								message = new Message();
								message.obj = "连接网络出错";
								message.what = 6;
								handler.sendMessage(message);
							} else {
								int prjid = Integer.valueOf(result);
								if (ibs != null) {
									
									if (bean != null
											&& bean.getPhoto1() != null) {
										for (int i = 0; i < ibs.length; i++) {
											String path = Environment
													.getExternalStorageDirectory()
													+ cityApplication.CameraPath;
											Log.i("System.out.p","图片路径"+path);
											if (ibs[i].isIsSelect()) {
												String urlpath = "supervisor/ashx/fileup.ashx";
												Map<String, String> maphs = new HashMap<String, String>();
												maphs.put("Prjcheckid",
														bean.getPrjcheckid());
												
												maphs.put("Name", ibs[i].getPathname());
												maphs.put("Prjid",Integer.valueOf(sentenceBean.getPrjid())+ "");
												maphs.put("Status",sentenceBean.getShushi()+ "");
												maphs.put("Flag",sentenceBean.getFlag()+ "");
												maphs.put("Superid",cityApplication.getUserId());
												maphs.put("Code",cityApplication.getUserCode());
												String result1 = HttpSendData.SendTextAndFile(urlpath,maphs,path+ "/"+ ibs[i].getPathname(),cityApplication.getHTTPURL());
												if (StringUtil.IsNull(result1)) {
													message = new Message();
													message.obj = "连接服务器出错";
													message.what = 6;
													handler.sendMessage(message);
												} else {
													int jgz = Integer
															.valueOf(result1);
													if (jgz == -1 || jgz == -2
															|| jgz == -3) {
														message = new Message();
														message.obj = "服务器出错";
														message.what = 6;
														handler.sendMessage(message);
													} else if (jgz == -4) {
														message = new Message();
														message.obj = "已在其他机器登录";
														message.what = 6;
														handler.sendMessage(message);
													} else {
														message = new Message();
														message.what = 5;
														message.getData()
														.putString(
																"msg",
																"");
														handler.sendMessage(message);
													}
												}
											}
										}
									} else {
										String urlimage = "supervisor/ashx/fileup.ashx";
										for (int i = 0; i < ibs.length; i++) {
											String path = Environment
													.getExternalStorageDirectory()
													+ cityApplication.CameraPath;
											if (ibs[i].isIsSelect()) {
												Map<String, String> params = new HashMap<String, String>();
												String imagename = ibs[i]
														.getPathname();
												params.put("Name", imagename);
												params.put("Prjid", prjid + "");
												params.put("status", "2");
												params.put("flag",Status.ProjectType_SB+ "");
												params.put("superid",cityApplication.getUserId());
												params.put("code",cityApplication.getUserCode());
												String jg = HttpSendData
														.SendTextAndFile(
																urlimage,
																params,
																path
																+ "/"
																+ ibs[i].getPathname(),cityApplication.getHTTPURL());
												if (StringUtil.IsNull(jg)) {
													message = new Message();
													message.obj = "连接服务器出错";
													message.what = 6;
													handler.sendMessage(message);
												} else {
													switch (Integer.valueOf(jg)) {
													case -1: {
														message = new Message();
														message.obj = "服务器出错";
														message.what = 6;
														handler.sendMessage(message);
														break;
													}
													case -2: {
														message = new Message();
														message.obj = "服务器出错";
														message.what = 6;
														handler.sendMessage(message);
														break;
													}
													case -3: {
														message = new Message();
														message.obj = "服务器出错";
														message.what = 6;
														handler.sendMessage(message);
														break;
													}
													case -4: {
														message = new Message();
														message.obj = "已在其他机器登录";
														message.what = 6;
														handler.sendMessage(message);
														break;
													}
													default: {
														message = new Message();
														message.what = 5;
														message.getData()
														.putString(
																"msg",
																"附件"
																		+ (i + 1)
																		+ ",上传成功");
														handler.sendMessage(message);
														break;
													}
													}
												}

											}
										}
									}
								}
							}
							SentenceBean sentenceupdateBean = null;
							if (saveid > 0) {
								sentenceupdateBean = sentenceDaoImp
										.get((int) saveid);
							} else {
								sentenceupdateBean = sentenceBean;
							}
							sentenceupdateBean.setSaveState(Status.SaveState_t2);
							sentenceDaoImp.update(sentenceupdateBean);

							message = new Message();
							message.what = 5;
							message.arg1 = 6;
							handler.sendMessage(message);
							break;
						}
						}
					}

				} catch (Exception e) {
					LogUtil.e(e);
				}

			}
		}
	}

	
	class Loctioning extends Thread{
		public Loctioning() {
			start();
		}
		
		@Override
		public void run() {
			try{
				double result=new Date().getTime()-cityApplication.getCreenDateTime();
				if(result<6000){
					Thread.sleep((long)(6000-result));
				}	
				handler.sendEmptyMessage(8);
			}catch(Exception e){LogUtil.e(e);}
		}
		
	}
	class Loading extends Thread {   //数据加载
		public Loading() {
			start();
		}

		@Override
		public void run() {
			if (cacheresult == null) {
				String url = "supervisor/ashx/gettype.ashx"; // 001 001
				Map<String, String> map = new HashMap<String, String>();
				map.put("superid", cityApplication.getUserId());
				map.put("code", cityApplication.getUserCode());
				try {
					String result = HttpSendData.SendGet(url, map, "utf-8",cityApplication.getHTTPURL());
					JSONArray jsonArray = new JSONArray(result);
					cacheresult = new ProblemBean[jsonArray.length()];
					problem_typestrings = new String[jsonArray.length()];
					for (int i = 0; i < jsonArray.length(); i++) {
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						cacheresult[i] = new ProblemBean();
						cacheresult[i].setName(jsonObject.getString("name"));
						cacheresult[i].setId(jsonObject.getInt("id"));
						problem_typestrings[i] = cacheresult[i].getName();
						String child = jsonObject.getString("child");
						if (StringUtil.IsNotNull(child)) {
							cacheresult[i].setChild(getChild(child));
						}
					}
					ProblemBean[] cachedale = cacheresult[0].getChild();
					problem_daleistrings = new String[cachedale.length];
					for (int i = 0; i < cachedale.length; i++) {
						problem_daleistrings[i] = cachedale[i].getName();
					}
					ProblemBean[] cachexiaolei = cachedale[0].getChild();
					problem_xiaoleistrings = new String[cachexiaolei.length];
					for (int i = 0; i < cachexiaolei.length; i++) {
						problem_xiaoleistrings[i] = cachexiaolei[i].getName();
					}

				} catch (Exception e) {
					LogUtil.e(e);
				}
			}
			Message message = new Message();
			message.what = 2;
			handler.sendMessage(message);
		}

		private ProblemBean[] getChild(String json) throws Exception {  //数据迭代
			JSONArray jsonArray = new JSONArray(json);
			ProblemBean[] beans = new ProblemBean[jsonArray.length()];
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				beans[i] = new ProblemBean();
				beans[i].setName(jsonObject.getString("name"));
				beans[i].setId(jsonObject.getInt("id"));
				try {
					String child = jsonObject.getString("child");
					if (StringUtil.IsNotNull(child)) {
						beans[i].setChild(getChild(child));
					}
				} catch (Exception e) {
				}
			}
			return beans;
		}

	}

	@Override
	public void onItemSelected(AdapterView<?> v, View view, int position,   
			long arg3) {
		if (!IsFuCha) {
			if (v == problem_type) {
				typepostion = position;
				ProblemBean[] beans = cacheresult[position].getChild();
				problem_daleistrings = new String[beans.length];
				for (int i = 0; i < beans.length; i++) {
					problem_daleistrings[i] = beans[i].getName();
				}
				problem_daleiadapter = new ArrayAdapter<String>(
						ProblemUploadActivity.this, R.layout.myspinner,
						problem_daleistrings);
				problem_daleiadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				problem_dalei.setAdapter(problem_daleiadapter);
				ProblemBean[] cachexiaolei = beans[0].getChild();
				problem_xiaoleistrings = new String[cachexiaolei.length];
				for (int i = 0; i < cachexiaolei.length; i++) {
					problem_xiaoleistrings[i] = cachexiaolei[i].getName();
				}
				problem_xiaoleiadapter = new ArrayAdapter<String>(
						ProblemUploadActivity.this, R.layout.myspinner,
						problem_xiaoleistrings);
				problem_xiaoleiadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				problem_xiaolei.setAdapter(problem_xiaoleiadapter);
				gettype(position);
			} else if (v == problem_dalei) {
				ProblemBean[] beans = cacheresult[typepostion].getChild();
				ProblemBean[] cachexiaolei = beans[position].getChild();
				problem_xiaoleistrings = new String[cachexiaolei.length];
				for (int i = 0; i < cachexiaolei.length; i++) {
					problem_xiaoleistrings[i] = cachexiaolei[i].getName();
				}
				problem_xiaoleiadapter = new ArrayAdapter<String>(
						ProblemUploadActivity.this, R.layout.myspinner,
						problem_xiaoleistrings);
				problem_xiaoleiadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				problem_xiaolei.setAdapter(problem_xiaoleiadapter);
				getdalei(position);
			}

		}
	}

	
	public void gettype(int position){   //设置类型下的大类
		
		typepostion = position;
		ProblemBean[] beans = cacheresult[position].getChild();
		problem_daleistrings = new String[beans.length];
		for (int i = 0; i < beans.length; i++) {
			problem_daleistrings[i] = beans[i].getName();
		}
		problem_daleiadapter = new ArrayAdapter<String>(
				ProblemUploadActivity.this, R.layout.myspinner,
				problem_daleistrings);
		problem_daleiadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		problem_dalei.setAdapter(problem_daleiadapter);
		ProblemBean[] cachexiaolei = beans[0].getChild();
		problem_xiaoleistrings = new String[cachexiaolei.length];
		for (int i = 0; i < cachexiaolei.length; i++) {
			problem_xiaoleistrings[i] = cachexiaolei[i].getName();
		}
		problem_xiaoleiadapter = new ArrayAdapter<String>(
				ProblemUploadActivity.this, R.layout.myspinner,
				problem_xiaoleistrings);
		problem_xiaoleiadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		problem_xiaolei.setAdapter(problem_xiaoleiadapter);
		
	}

	public void getdalei(int position){  //设置大类下的小类
		ProblemBean[] beans = cacheresult[typepostion].getChild();
		ProblemBean[] cachexiaolei = beans[position].getChild();
		problem_xiaoleistrings = new String[cachexiaolei.length];
		for (int i = 0; i < cachexiaolei.length; i++) {
			problem_xiaoleistrings[i] = cachexiaolei[i].getName();
		}
		problem_xiaoleiadapter = new ArrayAdapter<String>(
				ProblemUploadActivity.this, R.layout.myspinner,
				problem_xiaoleistrings);
		problem_xiaoleiadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		problem_xiaolei.setAdapter(problem_xiaoleiadapter);

	
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}

}
